package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.user.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {


    @Reference
    private UserService userServiceI;


    @GetMapping("sendSms")
    public Result sendSms(@RequestParam(name = "phone") String phone){
        Result result = new Result();
        // 验证手机号是否格式正确

        try {
            userServiceI.sendSms(phone);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(1);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    @PostMapping("save")
    public Result registerUser(String smsCode, @RequestBody User user){
        Result result = new Result();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        try {
            userServiceI.registerUser(smsCode, user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(1);
            result.setMessage(e.getMessage());
        }
        return result;
    }









}
